//
//  HZPeiziMoneyOutView.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/12/13.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziMoneyOutView.h"
#import "HZPeiziHeaderView.h"
#import "HZPeiziAddMoneyCell.h"

@interface HZPeiziMoneyOutView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UIView *pTopBgView;
@property (nonatomic, strong)UILabel *pTextLabel;
@property (nonatomic, strong)UITextField *pTextField;
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, strong)NSArray *pDataList;
@property (nonatomic, strong)HZPeiziHeaderView *pHeaderView;
@end

@implementation HZPeiziMoneyOutView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon
{
    _pTopBgView = [UIView new];
    _pTextLabel = [UILabel new];
    _pTextField = [UITextField new];
    _pTableView = [UITableView new];
    _pTableView.dataSource = self;
    _pTableView.delegate = self;
    _pTableView.tableFooterView = [UIView new];
    
    _pTableView.bounces = NO;
    _pTableView.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
    _pTopBgView.backgroundColor = [UIColor whiteColor];
    _pTextLabel.text = @"提取金额";
    _pTextLabel.font = [UIFont systemFontOfSize:19];
    _pTextField.placeholder = @"请输入要提取的金额";
    _pTextField.borderStyle = UITextBorderStyleRoundedRect;
    
    _pTextLabel.font = [UIFont systemFontOfSize:15];
    _pTextField.font = [UIFont systemFontOfSize:15];
}
- (void)initConstraints
{
    [self addSubview:_pTopBgView];
    [_pTopBgView addSubview:_pTextLabel];
    [_pTopBgView addSubview:_pTextField];
    [self addSubview:_pTableView];
    
    [_pTopBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(65);
    }];
    [_pTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_pTopBgView).offset(20);
        make.top.bottom.mas_equalTo(_pTopBgView);
        make.width.mas_equalTo(100);
    }];
    [_pTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_pTextLabel.mas_right);
        make.right.mas_equalTo(_pTopBgView).offset(-20);
        make.centerY.mas_equalTo(_pTopBgView);
        make.height.mas_equalTo(35);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTopBgView.mas_bottom).offset(20);
        make.left.right.bottom.mas_equalTo(self);
    }];
}

- (void)p_updateNewData:(NSArray *)data
{
    if (data && data.count > 0) {
        _pDataList = data;
        [_pTableView reloadData];
    }
}
- (NSString *)getOutMoney
{
    return _pTextField.text;
}
- (void)clearMoney
{
    _pTextField.text = @"";
}

#pragma mark -
#pragma mark UITableViewDataSource
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UITableViewCell *cell = nil;
    HZPeiziMoneyOutListModel *model = _pDataList[indexPath.row];
    cell = [HZPeiziAddMoneyCell cellTableView:tableView indexPath:indexPath cellModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (_pDataList.count > 0) {
        return self.pHeaderView;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (_pDataList.count > 0) {
        return 40;
    }
    return 0;
}

- (HZPeiziHeaderView *)pHeaderView {
    if (!_pHeaderView) {
        _pHeaderView = [HZPeiziHeaderView new];
        [_pHeaderView setTexts:@[@"申请日期",@"申请时间",@"提取金额",@"申请进度"]];
    }
    return _pHeaderView;
}

@end
